package com.zhoux.api.serialization;

import java.io.*;

/**
 * @author taoyi
 * @version 1.0
 * @date 2021/3/17 15:51
 *
 * 本类用于测试序列化和反序列化
 */
public class Test_Seri {
    public static void main(String[] args) {
//        method1(); // 测试序列化
        method2(); // 测试反序列化

    }


    // 创建用来测试序列化的方法
    public static void method1() {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(
                    new FileOutputStream("E:\\SoftwareEngineering\\java\\IO\\hello.txt"));
            Student s = new Student(9527,"zhoux",18,"1","2222");
            out.writeObject(s);
            System.out.println("序列化成功~");
        } catch (IOException e) {
            System.out.println("序列化失败~");
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 创建用来测试反序列化的方法
    public static void method2() {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(
                    new FileInputStream("E:\\SoftwareEngineering\\java\\IO\\hello.txt"));
            Object s = in.readObject();
            System.out.println(s);
            System.out.println("反序列化成功~");
        } catch (Exception e) {
            System.out.println("反序列化失败~");
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

